package com.rockfalldefence.sw.embark.client.page;

import net.binarymuse.gwt.client.ui.wizard.WizardPage;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.rockfalldefence.sw.embark.client.RDEmbarkContext;

public class TraiettoriaPage2 extends WizardPage<RDEmbarkContext> {

	public static final PageID PAGEID = new PageID();
	protected HorizontalPanel hPanel = new HorizontalPanel();
	protected FlowPanel panel;
	protected FlexTable t1;
	protected TextBox slAlpha; 
	protected TextBox tbBeta;
	protected TextBox tbMt;  
	protected TextBox tbBmin;  
	protected TextBox tbBmax;  
	protected TextBox tbHt;   
	protected TextBox tbHn;    
	protected TextBox tbEt; 
	protected TextBox tbPhit; 
	protected double diameter = 0;
	protected double energy = 0;
	protected double speed = 0;
	protected double volume = 0;
	protected double rho = 0;
	protected double mass = 0;
	
	@Override
	public void beforeShow() {
		super.beforeShow();
		RDEmbarkContext rdcntx = getContext();
		tbEt.setText( rdcntx.getE() );
		tbPhit.setText(rdcntx.getPhit());
		diameter = Double.parseDouble(rdcntx.getPhit());
		computeGammat();		
	}

	public TraiettoriaPage2() {
		panel = new FlowPanel();
	    panel.add(new HTML("<h1>Geometria del Rilevato</h1>"));
	    panel.add(new HTML("<p>Scegli i parametri " + 
	    		"necessari per la definizione della geometria del rilevato.</p>"));
	    
	    hPanel = new HorizontalPanel();
	    t1 = new FlexTable();
	    
	    Image img = new Image("images/trapezio_small.png");
	    t1.setWidget(0, 0, img);
	    Label lb = new Label("Energia del blocco (kJ)");
	    lb.setTitle("Energia");	    
	    tbEt = new TextBox();
	    tbEt.setWidth("100px");
	    tbEt.setReadOnly(true);
	    tbEt.setName("energia");
	    tbEt.setTitle("Energia del blocco");
	    FlexTable hpnl = new FlexTable();
	    hpnl.setWidget(0,0,lb);
	    hpnl.setWidget(0,1,tbEt);
	    
	    t1.setWidget(1, 0, hpnl);
	    hPanel.add(t1);
	  
	    t1 = new FlexTable();
	    
	    lb = new Label("Angolo gamma (gradi)");
	    lb.setTitle("Angolo d'incidenza sulla superficie del rilevato");
	    t1.setWidget(1, 0, lb);
	    tbBeta = new TextBox();
	    tbEt.setReadOnly(true);
	    
	    tbBeta.setWidth("80px");
	    
	    
	    tbBeta.setTitle("Angolo d'incidenza sulla superficie del rilevato");
	    tbBeta.setName("gammat");
	    tbBeta.setReadOnly(true);
	    //slider.setValue(65);
	    t1.setWidget(1, 1, tbBeta);
	    img = new Image("images/gamma.png");
	    img.setTitle("Angolo d'incidenza sulla superficie del rilevato");
	    t1.setWidget(1, 2, img);
	    
	    lb = new Label("Angolo alfa (gradi)");
	    lb.setTitle("Angolo del rilevato");
	    t1.setWidget(0, 0, lb);
	    
	    slAlpha = new TextBox();
	    slAlpha.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    slAlpha.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeGammat();
				computeBase();
			}
		});
	    slAlpha.setWidth("80px"); 
	    slAlpha.setTitle("Angolo del rilevato");
	    slAlpha.setName("alfa");
	    slAlpha.setText("65");
	    t1.setWidget(0, 1,slAlpha);
	    img = new Image("images/alfa.png");
	    img.setTitle("Angolo del rilevato");
	    t1.setWidget(0, 2, img);
	    
	    lb = new Label("H intercettazione (m)");
	    lb.setTitle("Altezza intercettazione della traiettoria del blocco");
	    t1.setWidget(2, 0, lb);
	    tbHt = new TextBox();
	    tbHt.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeHn();
				computeBase();
			}
		});
	    tbHt.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbHt.setWidth("80px");
	    tbHt.setName("ht");
	    tbHt.setTitle("Altezza intercettazione della traiettoria del blocco");
	    t1.setWidget(2, 1, tbHt);
	    img = new Image("images/ht.png");
	    img.setTitle("Altezza intercettazione della traiettoria del blocco");
	    t1.setWidget(2, 2, img);
	    

	       
	    lb = new Label("Diametro del blocco (m)");
	    lb.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(3, 0, lb);
	    tbPhit = new TextBox();
	    tbPhit.setWidth("80px");
	    tbPhit.setReadOnly(true);
	    tbPhit.setName("pit");
	    tbPhit.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(3, 1, tbPhit); 
	    img = new Image("images/phit.png");
	    img.setTitle("Diametro del blocco calcolato sulla base del volume");
	    t1.setWidget(3, 2, img);
	    
	    lb = new Label("H nominale (m)");
	    lb.setTitle("Altezza nominale del rilevato calcolato sulla base dei parametri inseriti");
	    t1.setWidget(4, 0, lb);
	    tbHn = new TextBox();
	    tbHn.setWidth("80px");
	    tbHn.setReadOnly(true);
	    tbHn.setName("hn");
	    tbHn.setTitle("Altezza nominale del rilevato calcolato sulla base dei parametri inseriti");
	    t1.setWidget(4, 1, tbHn);
	    img = new Image("images/hn.png");
	    img.setTitle("Altezza nominale del rilevato calcolato sulla base dei parametri inseriti");
	    t1.setWidget(4, 2, img);
	    
	    
	    lb = new Label("Largezza min piede (m)");
	    lb.setTitle("Larghezza minima del rilevato al piede");
	    t1.setWidget(6, 0, lb);
	    tbBmax = new TextBox();
	    tbBmax.setWidth("80px");
	    tbBmax.setReadOnly(true);
	    tbBmax.setName("bmax");
	    tbBmax.setTitle("Larghezza minima del rilevato al piede");
	    t1.setWidget(6, 1, tbBmax);
	    

	    lb = new Label("Largezza in testa (m)");
	    lb.setTitle("Larghezza del rilevato in testa");
	    t1.setWidget(5, 0, lb);
	    tbBmin = new TextBox();
	    tbBmin.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeBase();
			}
		});
	    tbBmin.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbBmin.setWidth("80px");
	    tbBmin.setName("bmin");
	    tbBmin.setTitle("Larghezza del rilevato in testa");
	    t1.setWidget(5, 1, tbBmin);
	    
	    
		  /*  
	    lb = new Label("Spin (rad/s)");
	    lb.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 0, lb);
	    tbOmegat = new TextBox();
	    tbOmegat.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				computeEt();
			}
		});
	    tbOmegat.addKeyPressHandler(new KeyPressHandler() {
	        public void onKeyPress(KeyPressEvent event) {
	          onlynumbers(event);
	        }
	    });
	    tbOmegat.setWidth("80px");
	    tbOmegat.setName("omegat");
	    tbOmegat.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 1, tbOmegat);
	    img = new Image("images/omegat.png");
	    img.setTitle("Velocità angolare in radianti al secondo");
	    t1.setWidget(7, 2, img);*/
	    hPanel.add(t1);
	    panel.add(hPanel);
	    panel.add(new HTML("<br/><p>Quando hai terminato di inserire i dati per continuare, prego fai " + 
		"click su \"Next.\"</p>"));
	}
	
	protected boolean computeHn()
	{
		String sValue = tbHt.getText();
		double dVal = Double.parseDouble(sValue);
		dVal = (dVal + diameter);
		long y=(long)(dVal*100);
		tbHn.setText(Double.toString((double)y/100));
		getContext().setHn(Double.toString((double)y/100));
		dVal = Double.parseDouble(sValue);
		y=(long)(dVal*100);
		getContext().setHt(Double.toString((double)y/100));
		return true;
	}
	
	protected boolean onlynumbers(KeyPressEvent event)
	{

		char inputC = event.getCharCode();
		Character chr = new Character(inputC);
		if (Character.isLetter(inputC)) {
			((TextBox) event.getSource()).cancelKey();
		} else if (chr.compareTo(new Character(','))==0) {
			((TextBox) event.getSource()).cancelKey();
		}
		return true;
	}
	
	protected void computeBase() {
		double c1 = Double.parseDouble(tbHn.getText());
		double a = Double.parseDouble(slAlpha.getText());
		double c2 = c1*(Math.cos(Math.toRadians(a)) / Math.sin(Math.toRadians(a)));
		if (tbBmin.getText().isEmpty()) return;
		double bmin1 = Double.parseDouble(tbBmin.getText());
		c2 = 2*c2 + bmin1;
		long x = (long)(c2*100);
		tbBmax.setText(Double.toString((double)x/100));		
	}
	
	protected void computeGammat() {
		String sValue = slAlpha.getText();
		double dVal = Double.parseDouble(sValue);
		double betaVal = Double.parseDouble(getContext().getBetat());
		double res = 180.0-(dVal+betaVal);
		long x = (long)(res*100);
		tbBeta.setText(Double.toString((double)x/100));
	}
	
	@Override
	public Widget asWidget() {     
		
		return panel;
	}
	
	@Override
	public PageID getPageID() {
		return PAGEID;
	}

	@Override
	public String getTitle() {
		
		return "Geometria del rilevato";
	}

}
